﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITAP.ShprossesBuilder.Properties;
using ITAP.ShprossesBuilder.Modelling.Components;

namespace ITAP.ShprossesBuilder.Operations
{
    public class OperationJoinShprossesEnds : IOperation
    {

        #region IOperation Members

        public string Name
        {
            get { return "Состыковать концы профилей"; }
        }

        public OperationGroup OperationGroup
        {
            get { return OperationGroup.ShprossOperation; }
        }

        public System.Drawing.Image Thumbnail
        {
            get { return Resources.JoinShprosses.ToBitmap(); }
        }

        public void Execute(ModelControl modelControl)
        {
            Shpross firstShpross = null;
            Shpross secondShpross = null;
            int counter = 0;
            foreach (IComponent selectedComponent in modelControl.ModelContainer.SelectedComponentsStack)
            {
                if (selectedComponent is Shpross)
                {
                    if (counter == 0)
                    {
                        firstShpross = selectedComponent as Shpross;
                        counter++;
                    }
                    else if (counter == 1)
                    {
                        secondShpross = selectedComponent as Shpross;
                        counter++;
                    }
                }
            }
            if (firstShpross == null || secondShpross == null)
                return;

            if (firstShpross.SelectedPoint == ShprossPoints.Start)
            {
                if (secondShpross.SelectedPoint == ShprossPoints.Start)
                    secondShpross.MoveStartPointTo(firstShpross.StartPoint, secondShpross.Parent.BoundingBox, false);
                else if (secondShpross.SelectedPoint == ShprossPoints.End)
                    secondShpross.MoveEndPointTo(firstShpross.StartPoint, secondShpross.Parent.BoundingBox, false);
            }
            else if(firstShpross.SelectedPoint == ShprossPoints.End)
            {
                if (secondShpross.SelectedPoint == ShprossPoints.Start)
                    secondShpross.MoveStartPointTo(firstShpross.EndPoint, secondShpross.Parent.BoundingBox, false);
                else if (secondShpross.SelectedPoint == ShprossPoints.End)
                    secondShpross.MoveEndPointTo(firstShpross.EndPoint, secondShpross.Parent.BoundingBox, false);
            }                
        }

        public bool CanUndo
        {
            get { return true; }
        }

        #endregion
    }
}
